﻿using IMark.DAO;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IMark
{
    public partial class CS : System.Web.UI.Page
    {
        [System.Web.Services.WebMethod]
        public static bool LaySanPhamTheoLoai(string Ten, string Email, string NoiDung, string Sao,string IDSP)
        {
            return SanPhamModels.DanhGia(Ten,Email,NoiDung,Sao,IDSP);
        }

        [System.Web.Services.WebMethod]
        public static string DangNhap(string User, string Password)
        {
            KHACH_HANG kh = KhachHangModels.DangNhap(User, Utils.ConvertMD5(Password));
            if (kh != null)
                return kh.HoTen;
            else
                return "false";
        }

        [System.Web.Services.WebMethod]
        public static bool DangKyEmail(string Email)
        {
            DANG_KY_NHAN_MAIL dk = new DANG_KY_NHAN_MAIL();
            dk.Email = Email;
            //dk.HoTen = kh.HoTen;
            dk.TrangThai = 1;
            bool result = Utils.DangKyMailNhanTin(dk);
            return result;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if(Request.QueryString["ID"]!=null)
            {
                EMartEntities db = null;
                try
                {
                    db = new EMartEntities();
                    long ID = int.Parse(Request.QueryString["ID"]);
                    int SoLuong = int.Parse(Request.QueryString["SL"]);
                    //Lay gio hang tu session
                    Dictionary<long, SAN_PHAM> Cart = Session["Cart"] as Dictionary<long, SAN_PHAM>;
                    long ThanhTien = 0;
                    // da co -> tang so luong len 1
                    if (Cart.ContainsKey(ID))
                    {
                        SAN_PHAM h = Cart[ID] as SAN_PHAM;
                        if (SoLuong == 0)
                            h.SoLuong += 1;
                        else
                            h.SoLuong = h.SoLuong + SoLuong;

                    }
                    // chua co -> truy van tu CSDL va bo vao gio
                    else
                    {
                        SAN_PHAM h = db.SAN_PHAM.Single(hh => hh.ID == ID);
                        if (SoLuong == 0)
                            h.SoLuong = 1;
                        else
                            h.SoLuong = SoLuong;
                        Cart.Add(ID, h);
                    }
                    Response.Write(SanPhamModels.GenerateCartInfo(Cart) + ";" + String.Format(CultureInfo.InvariantCulture, "{0:0,0}", SanPhamModels.TongThanhTienGioHang(Cart)) + ";" + Cart.Count());
                }
                catch
                {
                    Response.Write(false);
                }
                finally
                {
                    if (db != null)
                        db.Dispose();
                }
            }

            if(Request.QueryString["RemoveCart"]!=null)
            {
                try {
                    long ID = long.Parse(Request.QueryString["RemoveCart"].ToString());
                    Dictionary<long, SAN_PHAM> Cart = Session["Cart"] as Dictionary<long, SAN_PHAM>;
                    if (Cart.ContainsKey(ID))
                    {
                        SAN_PHAM h = Cart[ID] as SAN_PHAM;
                        if (h.SoLuong > 1)
                        {
                            h.SoLuong = h.SoLuong - 1;
                        }
                        else
                        {
                            Cart.Remove(ID);
                        }
                        Response.Write(SanPhamModels.GenerateCartInfo(Cart) + ";" + String.Format(CultureInfo.InvariantCulture, "{0:0,0}", SanPhamModels.TongThanhTienGioHang(Cart)) + ";" + Cart.Count());
                    }
                }
                catch
                {
                    Response.Write(false);
                }
            }

            if(Request.QueryString["InsertWish"]!=null)
            {
                if(Request.QueryString["IDSP"]!=null)
                {
                    EMartEntities db = new EMartEntities();
                    KHACH_HANG kh = Session["User"] as KHACH_HANG;
                    long ID = long.Parse(Request.QueryString["IDSP"]);
                    //Check exist
                    var queryExist = db.SAN_PHAM_YEU_THICH.Where(w => w.IDSanPham == ID && w.IDKH == kh.ID);
                    if(queryExist.Count()==0)
                    {
                        try
                        {
                            SAN_PHAM_YEU_THICH spyt = new SAN_PHAM_YEU_THICH();
                            spyt.IDKH = kh.ID;
                            spyt.IDSanPham = ID;
                            db.SAN_PHAM_YEU_THICH.Add(spyt);
                            db.SaveChanges();
                            Response.Write(true);
                        }
                        catch
                        {
                            Response.Write(false);
                        }
                    }
                    else
                    {
                        Response.Write("Exist");
                    }
                }
            }
        }
    }
}